package org.spring.springboot.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.domain.City;
import org.spring.springboot.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CityRestController {
	private final static Logger logger = LoggerFactory.getLogger(CityRestController.class);
	@Autowired
	private CityService cityService;

	@RequestMapping(value = "/api/city/all", method = RequestMethod.GET)
	public List<City> findOneCity() {
		return cityService.findCityAll();
	}
	@RequestMapping(value = "/api/city/get/{id}", method = RequestMethod.GET)
	public City findOneCity(@PathVariable(value = "id", required = true) long id,
			HttpServletRequest request) throws Exception {
		logger.info(request.getRequestURL().toString());
		request.getSession().setAttribute("request Url", request.getRequestURL());
		City city=cityService.getCityById(id);
		if(city==null){
			throw new Exception("Not Found City");
		}
		return city;
	}
	
	@RequestMapping(value = "/api/city/find", method = RequestMethod.GET)
	public City findOneCity(@RequestParam(value = "cityName", required = true) String cityName,
			HttpServletRequest request) throws Exception {
		logger.info(request.getRequestURL().toString());
		request.getSession().setAttribute("request Url", request.getRequestURL());
		City city=cityService.findCityByName(cityName);
		if(city==null){
			throw new Exception("Not Found City");
		}
		return city;
	}

	@RequestMapping(value = "/api/city/find", method = RequestMethod.POST, consumes = "application/json")
	public List<City> findCity(@RequestBody City city) throws Exception {
		List<City> list=cityService.findCityMore(city);
		if(list.size()<1){
			throw new Exception("Not Found City");
		}
		return list;
	}

	@RequestMapping(value = "/api/city/add", method = RequestMethod.POST, consumes = "application/json")
	public Long addCity(@RequestBody City city) throws Exception {
		return cityService.addCity(city);
	}

	@RequestMapping(value = "/api/city/del", method = RequestMethod.GET)
	public Long delCity(@RequestParam(value = "id", required = true) Long id) {
		return 0L;
	}

	@RequestMapping(value = "/api/city/del", method = RequestMethod.POST, consumes = "application/json")
	public Long delCity(@RequestBody City city) {
		return 0L;
	}

}
